import {defineStore} from 'pinia';
import {ChatState,Message} from '/@/types/aigc/chat';
import {formatToDateTime} from '/@/utils/formatTime';

// 定义store
export const useChatStore = defineStore('chatStore', {
    state: ():ChatState=> ({
        appId:'',
        conversationId:'',
        modelId:'',
        modelName:'',
        modelProvider:'',
        messages: [],
    }),
    getters:{},
    actions:{
        addMessage(message:string,role:'user'|'assistant'|'system',chatId:string){
            const newMessage: Message = {chatId:chatId,role:role,message:message,createTime: formatToDateTime(new Date())};
            this.messages.push(newMessage);
        },
        // 更新消息
        updateMessage(chatId: string, message: string, isError?: boolean){
            const index = this.messages.findIndex((item) => item?.chatId === chatId);
            if (index !== -1) {
                this.messages[index].message = message;
                if(isError){
                    this.messages[index].isError=isError;
                }
            }
        },
        // 清空消息
        deleteMessage(chatId:string){
            this.messages = this.messages.filter((item) => item?.chatId !== chatId);
        },
    },
});